/*
 * Copyright 2007-208 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package com.cassevern.grails.util

class Build400GrailsDomainTest extends GroovyTestCase {
	def ALL_TYPES_DOMAIN = 
"""class AllTypes implements Serializable {
	BigDecimal packedDecimal
	int zonedDecimal
	Double float9
	Short bin1
	Short bin2
	Short bin4
	int bin8
	Long bin16
	String char20
	Date datel
	Date timet
	Date timstp
	static constraints = {
		packedDecimal(max:new BigDecimal("9999999.99"),nullable:false)
		zonedDecimal(max:99999,nullable:false)
		float9(nullable:false)
		bin1(nullable:false)
		bin2(nullable:false)
		bin4(nullable:false)
		bin8(nullable:false)
		bin16(nullable:false)
		char20(maxSize:20,nullable:false)
		datel(nullable:false)
		timet(nullable:false)
		timstp(nullable:false)
	}
	static mapping = {
		table ('alltypes')
		version (false)
		id (generator:'assigned')
		id (composite:['packedDecimal','zonedDecimal'])
		columns {
			id (composite:['packedDecimal','zonedDecimal'])
			packedDecimal (column:'PCKDEC', type:'big_decimal')
			zonedDecimal (column:'ZONDEC', type:'int')
			float9 (column:'FLOAT9', type:'double')
			bin1 (column:'BIN1', type:'short')
			bin2 (column:'BIN2', type:'short')
			bin4 (column:'BIN4', type:'short')
			bin8 (column:'BIN8', type:'int')
			bin16 (column:'BIN16', type:'long')
			char20 (column:'CHAR20', type:'string')
			datel (column:'DATEL', type:'date')
			timet (column:'TIMET', type:'time')
			timstp (column:'TIMSTP', type:'timestamp')
		}
	}
	boolean newEntity = false
	static transients = ['newEntity']
}"""

def QCUSTCDT_DOMAIN = 
"""class Customer implements Serializable {
	String lastNameField
	String firstAndMiddleInitialField
	String streetAddressField
	String cityField
	String stateAbbreviationField
	int zipCodeField
	int creditLimitField
	int chargeCodeField
	BigDecimal balanceDueField
	BigDecimal creditDueField
	static constraints = {
		lastNameField(maxSize:8,nullable:false)
		firstAndMiddleInitialField(maxSize:3,nullable:false)
		streetAddressField(maxSize:13,nullable:false)
		cityField(maxSize:6,nullable:false)
		stateAbbreviationField(maxSize:2,nullable:false)
		zipCodeField(max:99999,nullable:false)
		creditLimitField(max:9999,nullable:false)
		chargeCodeField(max:9,nullable:false)
		balanceDueField(max:new BigDecimal("9999.99"),nullable:false)
		creditDueField(max:new BigDecimal("9999.99"),nullable:false)
	}
	static mapping = {
		table ('qcustcdt')
		version (false)
		id (generator:'assigned')
		columns {
			id (column:'CUSNUM')
			lastNameField (column:'LSTNAM', type:'string')
			firstAndMiddleInitialField (column:'INIT', type:'string')
			streetAddressField (column:'STREET', type:'string')
			cityField (column:'CITY', type:'string')
			stateAbbreviationField (column:'STATE', type:'string')
			zipCodeField (column:'ZIPCOD', type:'int')
			creditLimitField (column:'CDTLMT', type:'int')
			chargeCodeField (column:'CHGCOD', type:'int')
			balanceDueField (column:'BALDUE', type:'big_decimal')
			creditDueField (column:'CDTDUE', type:'big_decimal')
		}
	}
	boolean newEntity = false
	static transients = ['customerNumberField', 'newEntity']
	int getCustomerNumberField() {
		if (id) return id
		return 0
	}
	void setCustomerNumberField (int vlu) { id = vlu }
}"""

	void testMappingDSLGenerator () { 
		
		Build400GrailsDomain build = new Build400GrailsDomain(
				ip:'172.16.1.55', user:'dgd', password:'vo2max', library:'dgd', file:'alltypes', domainName:'AllTypes', verbose:true)
		build.retrieveTableAttributes() 	
		build.retrieveIndex () 

		ALL_TYPES_DOMAIN = ALL_TYPES_DOMAIN.replaceAll(/\t|\r|\n/,' ')
		def domain = build.createDomainClass()
		domain = domain.replaceAll(/\t|\r|\n/,' ')
		assertEquals (ALL_TYPES_DOMAIN, domain)

		build = new Build400GrailsDomain(
				ip:'172.16.1.55', user:'dgd', password:'vo2max', library:'qiws', file:'qcustcdt', domainName:'Customer', verbose:true)
		build.retrieveTableAttributes() 	
		build.retrieveIndex () 
		domain = build.createDomainClass()
		QCUSTCDT_DOMAIN = QCUSTCDT_DOMAIN.replaceAll(/\t|\r|\n/,' ')
		domain = domain.replaceAll(/\t|\r|\n/,' ')
		assertEquals (QCUSTCDT_DOMAIN, domain)
	}
}